package service;

import dao.NewsDao;
import dao.NewsDaoImp;
import pojo.News;
import pojo.NewsPage;

import java.util.List;
import java.util.Map;

public class NewsServiceImpl implements NewsService{
    private NewsDao newsDao = new NewsDaoImp();
    @Override
    public NewsPage getAllNews(Map<String,Object> params) {
        Integer page = (Integer) params.get("page");
        Integer size = (Integer) params.get("size");
        Integer start = (page - 1) * size;
        params.put("start",start);
        params.put("length",size);
        List<News> news = newsDao.selectAllNews(params);
        Integer count = newsDao.selsectNewsCount(params);
        Integer total = (count % size == 0)? count/size : count/size +1;
        NewsPage newsPage = new NewsPage();
        newsPage.setNewsList(news);
        newsPage.setRowCount(count);
        newsPage.setPageCount(total);
        newsPage.setPageNum(page);
        newsPage.setPageSize(size);
        return newsPage;
    }

    @Override
    public News getNewsById(Integer newsId) {
        return newsDao.selectNewsById(newsId);
    }

    @Override
    public Integer setViewsById(News news) {
        return newsDao.updateViewsById(news);
    }
}
